unit PChildPngForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TParentPngForm = class(TForm);

  TChildPngForm = class(TForm)
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormClick(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    id: Integer;
    xPos: Integer;
    yPos: Integer;
    parentForm: TParentPngForm;
  public
    constructor Create(AOwner: TObject; idComponent: Integer; dirImage: String; leftF, topF: Integer);
    procedure setParameters(AOwner: TObject; dirImage: String; leftF, topF: Integer);
    function GetRect(AOwner: TObject; xPos,yPos,xkPos,ykPos: Integer): TRect;
    function getXPos: Integer;
    function getYPos: Integer;
    function getID: Integer;
    function getParentForm: TParentPngForm;
    procedure setID(idF: Integer);
    procedure setXPos(x: Integer);
    procedure setYPos(y: Integer);
    procedure WMWindowPosChanging(var Msg: TWMWindowPosChanging);
  end;             

var
  ChildPngForm: TChildPngForm;
  hbmp: HBITMAP;
  backdc: HDC;

implementation

uses GDIPAPI, GDIPOBJ, PAuthorizationForm;

{$R *.dfm}

constructor TChildPngForm.Create(AOwner: TObject; idComponent: Integer; dirImage: String; leftF, topF: Integer);
begin
  inherited Create(nil);

  id := idComponent;
  xPos := leftF;
  yPos := topF;
  parentForm := TParentPngForm(AOwner);
  setParameters(AOwner, dirImage, xPos, yPos);
  SetWindowPos(Self.Handle,HWND_TOPMOST, Left,Top,Width,Height, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
end;

procedure TChildPngForm.FormDestroy(Sender: TObject);
begin
  AuthorizationForm.delFormForArray(id);
end;

function TChildPngForm.GetRect(AOwner: TObject; xPos, yPos, xkPos, ykPos: Integer): TRect;
var rect: TRect;
begin
  rect.Left := TAuthorizationForm(AOwner).BoundsRect.Left;
  rect.Top := TAuthorizationForm(AOwner).BoundsRect.Top;                            

  rect.Left := rect.Left + xPos;
  rect.Top := rect.Top + yPos;
  rect.Right := rect.Left + xkPos;
  rect.Bottom := rect.Top + ykPos;

  Result := rect;
end;

function TChildPngForm.getXPos: Integer;
begin
  Result := xPos;
end;

function TChildPngForm.getYPos: Integer;
begin
  Result := yPos;
end;

procedure TChildPngForm.setParameters(AOwner: TObject; dirImage: String; leftF, topF: Integer);
var
  img: TGPBitmap;
  screendc: HDC;
  pt1, pt2 : TPoint;
  sz : TSize;
  bf : TBlendFunction;
begin
  Self.BorderStyle := bsNone;

  if dirImage <> '' then
  begin

  if SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED) = 0 then
    ShowMessage(SysErrorMessage(GetLastError));

  img := TGPBitmap.Create(dirImage,True);
  with img do begin
    GetHBITMAP(0,hbmp);
    Width := GetWidth;
    Height := GetHeight;
    Free;
  end;
  screendc := GetDC(0);
  backdc := CreateCompatibleDC(screendc);
  SelectObject(backdc, hbmp);
  pt1 := Point(Left, Top);
  pt2 := Point(0,0);
  sz.cx := Width;
  sz.cy := Height;
  with bf do begin
    BlendOp := AC_SRC_OVER;
    BlendFlags := 0;
    SourceConstantAlpha := $FF;
    AlphaFormat := AC_SRC_ALPHA;
  end;

  UpdateLayeredWindow(Handle, screendc, @pt1, @sz, backdc, @pt2,0, @bf,ULW_ALPHA);
  ReleaseDC(0,screendc);

  end;

  Self.BoundsRect := GetRect(AOwner, leftF, topF, Width, Height);
end;

procedure TChildPngForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caNone;
end;

procedure TChildPngForm.FormClick(Sender: TObject);
begin
  Windows.SetFocus(parentForm.Handle);
end;

procedure TChildPngForm.WMWindowPosChanging(var Msg: TWMWindowPosChanging);
begin
  SetWindowPos(Handle,HWND_TOPMOST,Left,Top,Width,Height, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
  Application.Restore;
end;

procedure TChildPngForm.setID(idF: Integer);
begin
  id := idF;
end;

procedure TChildPngForm.setXPos(x: Integer);
begin
  xPos := x;
end;

procedure TChildPngForm.setYPos(y: Integer);
begin
 yPos := y;
end;

function TChildPngForm.getID: Integer;
begin
  Result := id;
end;

function TChildPngForm.getParentForm: TParentPngForm;
begin
  Result := parentForm;
end;

procedure TChildPngForm.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  //
end;

end.
